package com.zzy.chapter4.dutychain;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.zzy.chapter4.pojo.BusinessLaunch;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class CityHandler extends AbstractBusinessHandler {
    @Override
    public List<BusinessLaunch> processHandler(List<BusinessLaunch> businessLaunches, String targetCity, String targetSex, String targetProduct) {
        if (CollectionUtil.isEmpty(businessLaunches)) {
            return businessLaunches;
        }
        businessLaunches = businessLaunches.stream().filter(businessLaunch -> {
            String city = businessLaunch.getTargetCity();
            if(StrUtil.isBlank(city)) {
                return true;
            }
            List<String> cities = Arrays.asList(city.split(","));
            return cities.contains(targetCity);
        }).collect(Collectors.toList());
        if (hasNextHandler()) {
            return nextHandler.processHandler(businessLaunches, targetCity, targetSex, targetProduct);
        }
        return businessLaunches;
    }
}
